﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using PPI.Client.Logic.Distribution;
using PPI.Entities.Distribution;

namespace PPI.Controls.Grid
{
    public partial class GridDistribution : GridBase
    {
        private List<EDistributionPole> m_Poles;
        public DistributionType DistributionType;

        public List<EDistributionPole> CurrentPoles
        {
            get { return m_Poles; }
        }

        public GridDistribution()
        {
            InitializeComponent();
        }

        protected override void AfterOpenMenu()
        {
            base.AfterOpenMenu();

            cmiAssociateUtility.Visible = false;
            cmiEditWhithOutImages.Visible = false;
        }


        public void LoadPoles(string connectionString)
        {
            m_Poles = LPole.GetDistributionPolesWithDetails(connectionString);
            RefreshGrid();
        }

        public void RefreshGrid()
        {
            DataTable dt = new DataTable("Inspection");
            DataColumn asw = new DataColumn();
            
            dt.Columns.Add(new DataColumn("Id", typeof(Int32)));
            dt.Columns.Add(new DataColumn("Map", typeof(String)));
            dt.Columns.Add(new DataColumn("Section", typeof(String)));

            if (DistributionType == Entities.Distribution.DistributionType.Inspection)
            {
                dt.Columns.Add(new DataColumn("Inspection", typeof(String)));
                dt.Columns.Add(new DataColumn("Result", typeof(String)));
                dt.Columns.Add(new DataColumn("Electrical", typeof(String)));
                dt.Columns.Add(new DataColumn("Notes", typeof(String)));
                dt.Columns.Add(new DataColumn("Comments", typeof(String)));
            }

            dt.Columns.Add(new DataColumn("Specie", typeof(String)));
            dt.Columns.Add(new DataColumn("Treatment", typeof(String)));
            dt.Columns.Add(new DataColumn("Class", typeof(String)));
            dt.Columns.Add(new DataColumn("Lenght", typeof(String)));
            dt.Columns.Add(new DataColumn("Year", typeof(Int32)));
//            dt.Columns.Add(new DataColumn("IdInspectionResult", typeof(Int32)));
            dt.Columns.Add(new DataColumn("Latitude", typeof(double)));
            dt.Columns.Add(new DataColumn("Longitude", typeof(double)));
            dt.Columns.Add(new DataColumn("InspBy", typeof(string)));
            dt.Columns.Add(new DataColumn("Number", typeof(string)));
            dt.Columns.Add(new DataColumn("Kv", typeof(string)));
            dt.Columns.Add(new DataColumn("Circuit", typeof(string)));
            dt.Columns.Add(new DataColumn("Has Transformers?", typeof(string)));
            dt.Columns.Add(new DataColumn("AcquisitionDate", typeof(DateTime)));
            dt.Columns.Add(new DataColumn("LVisit", typeof(Int32)));
            
            StringBuilder poleNotes;
            foreach (EDistributionPole pole in m_Poles)
                if (DistributionType == Entities.Distribution.DistributionType.Inspection) 
                {
                     poleNotes = new StringBuilder();
                    
                    foreach (ENote note in pole.Notes)
                        poleNotes.Append(note.Name + ", ");

                    if (pole.Notes.Count > 0)
                        poleNotes = poleNotes.Remove(poleNotes.ToString().LastIndexOf(","), 1);

                    dt.Rows.Add(
                        pole.Id,
                        pole.Map.Name,
                        pole.Section.Name,
                        pole.InspectionResult.InspectionType.Name,
                        pole.InspectionResult.Resultype.Name,
                        pole.InspectionResult.IsElectrical ? "YES" : "NO",
                        poleNotes.ToString().Trim(),
                        pole.InspectionResult.Comments.Trim(),
                        pole.Specie.Name, 
                        pole.Treatment.Name,
                        pole.Class.Name, 
                        pole.Length.Name, 
                        pole.Year == Int32.MinValue ? 0 : pole.Year,
                        //pole.InspectionResult.Id,
                        pole.Latitude == double.MinValue ? 0 : pole.Latitude,
                        pole.Longitude == double.MinValue ? 0 : pole.Longitude,
                        pole.InspBy, 
                        pole.Number, 
                        pole.Kv.Text,
                        pole.Circuit,
                        (pole.Transformers != null && pole.Transformers.Count > 0) ? "YES" : "NO",
                        pole.AcquisitionDate,
                        pole.LVisit.Year                        
                        );
                }
                else
                {
                    dt.Rows.Add(
                        pole.Id,
                        pole.Map.Name,
                        pole.Section.Name,
                        pole.Specie.Name,
                        pole.Treatment.Name,
                        pole.Class.Name,
                        pole.Length.Name,
                        pole.Year == Int32.MinValue ? 0 : pole.Year,
                        pole.InspectionResult.Id,
                        pole.Latitude == double.MinValue ? 0 : pole.Latitude,
                        pole.Longitude == double.MinValue ? 0 : pole.Longitude,
                        pole.InspBy,
                        pole.Number,
                        pole.Kv.Text,
                        pole.Circuit,
                        pole.AcquisitionDate,
                        pole.LVisit.Year                        
                        );
                }
            
            datagridBase.AutoGenerateColumns = true;
            datagridBase.DataSource = dt;
            //datagridBase.Refresh();

            //HIDE ID COLUMN
            datagridBase.Columns[0].Width = 0;
            datagridBase.Refresh();
        }
        private void OnClickedExportToExcel(object o, EventArgs e)
        {
            // Safely invoke an event:
            if (ClickedExportToExcel != null)
            {
                ClickedExportToExcel(o, e);
            }
        }
        public event EventHandler ClickedExportToExcel;
        private void cmiExportToExcel_Click(object sender, EventArgs e)
        {
            OnClickedExportToExcel(this, new EventArgs());
        }

    }
}

